package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.annotation.AutoFill;
import com.sky.constant.MessageConstant;
import com.sky.constant.StatusConstant;
import com.sky.context.BaseContext;
import com.sky.dto.DishDTO;
import com.sky.dto.DishPageQueryDTO;
import com.sky.entity.Dish;
import com.sky.entity.DishFlavor;
import com.sky.exception.DeletionNotAllowedException;
import com.sky.mapper.DishFlavorMapper;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealDishMapper;
import com.sky.result.PageResult;
import com.sky.service.DishService;
import com.sky.vo.DishVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

@Service
public class DishServiceImpl implements DishService {
    @Autowired
    private DishMapper dishMapper;
    @Autowired
    private DishFlavorMapper dishFlavorMapper;
    @Autowired
    private SetmealDishMapper setmealDishMapper;
    /**
     * 新增菜品
     */

    @Override
    public void addDishwithFlavor(DishDTO dishDTO) {
        Dish dish = new Dish();
        BeanUtils.copyProperties(dishDTO,dish);

        //不引入dto因为dto里包含了口味
        dishMapper.insertDish(dish);//向菜品表中插入一条数据

        Long dishId = dish.getId();//获取insert数据库后的菜品id

        List<DishFlavor> flavors = dishDTO.getFlavors();

        if(flavors!=null && flavors.size()>0){
            //每个口味词条都附上菜品的id作为其id
            //写法1
//            for(DishFlavor dishFlavor:flavors){
//                dishFlavor.setDishId(dishId);
//            }

            //写法2
            flavors.forEach(dishFlavor -> {dishFlavor.setDishId(dishId);});

            //向口味表中插入多条数据
            dishFlavorMapper.insertBatch(flavors);

        }
    }

    @Override
    public PageResult dishPage(DishPageQueryDTO dishPageQueryDTO) {
        PageHelper.startPage(dishPageQueryDTO.getPage(), dishPageQueryDTO.getPageSize());
        Page<DishVO> page=dishMapper.pageQuery(dishPageQueryDTO);
        return new PageResult(page.getTotal(),page.getResult());
    }

    @Override
    @Transactional
    public void deleteDishwithFlavor(List<Long> ids) {
        //如果是起售的菜品不能删
        for(Long id:ids){
            Integer dishId=dishMapper.checkDishStatusById(id);
            if (dishId== StatusConstant.ENABLE){
                throw new DeletionNotAllowedException(MessageConstant.DISH_ON_SALE);
            }
        //在套餐里面的不能删
            Integer setmealId=setmealDishMapper.checkDishinSetmealById(id);
            if(setmealId!=null){
                throw new DeletionNotAllowedException(MessageConstant.DISH_BE_RELATED_BY_SETMEAL);
            }
        //根据id删除菜品
            dishMapper.deleteDish(id);
        //删除与菜品关联的口味表的数据
            dishFlavorMapper.deleteFlavorByDishId(id);
        }
    }

    @Override
    public DishVO getDishwithFlavors(Long id) {
        //根据id查询菜品
        Dish dish =dishMapper.getDishById(id);
        //根据id查询口味
        List<DishFlavor> dishFlavor= dishFlavorMapper.getFlavorById(id);
        //封装成VO
        DishVO dishVO=new DishVO();
        BeanUtils.copyProperties(dish,dishVO);
        dishVO.setFlavors(dishFlavor);
        return dishVO;
    }

    @Override
    @Transactional
    public void updateDishwithFlavors(DishDTO dishDTO) {
        //修改菜品表
        Dish dish= new Dish();//先把属于菜品的信息改了
        BeanUtils.copyProperties(dishDTO,dish);
        dishMapper.updateDish(dish);//应该是修改id对应的菜品信息啊,但是因为DTO里有id，属性也拷贝给了dish，所以dish能拿到id
        //删除原有口味表
        dishFlavorMapper.deleteFlavorByDishId(dish.getId());
        //插入最新口味数据
        //因为口味与菜品关联了，所以插入新口味时得关联菜品id
        List<DishFlavor> flavors = dishDTO.getFlavors();
        for(DishFlavor dishFlavor:flavors){
                dishFlavor.setDishId(dishDTO.getId());
            }
        dishFlavorMapper.insertBatch(flavors);
    }

    /**
     * 条件查询菜品和口味
     * @param dish
     * @return
     */
    public List<DishVO> listWithFlavor(Dish dish) {
        List<Dish> dishList = dishMapper.list(dish);

        List<DishVO> dishVOList = new ArrayList<>();

        for (Dish d : dishList) {
            DishVO dishVO = new DishVO();
            BeanUtils.copyProperties(d,dishVO);

            //根据菜品id查询对应的口味
            List<DishFlavor> flavors = dishFlavorMapper.getFlavorById((d.getId()));

            dishVO.setFlavors(flavors);
            dishVOList.add(dishVO);
        }

        return dishVOList;
    }

    /**
     * 根据分类id查询菜品
     * @param categoryId
     * @return
     */
    public List<Dish> list(Long categoryId) {
        Dish dish = Dish.builder()
                .categoryId(categoryId)
                .status(StatusConstant.ENABLE)
                .build();
        return dishMapper.list(dish);
    }
}
